﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using Models.Constant;
using ServiceWeb.DBGateway;
using ServiceWeb.WebClient;
using System.Reflection.Emit;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using FileServer.Common;
namespace FileServer.Controllers
{
    public class VariableController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = "    ";
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and v_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and v_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select  * from iot_device_model_variable where 1=1  " + sqlwhere;
            DataTable dt = MySqlYihongFileServer.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "v_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel, iot_product product)
        {
            if (httpReqModel.order == "upd")
            {
                ViewBag.edit_guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.edit_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }

        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpReqModel, iot_product product)
        {
            product.p_c_guid = LoginUserInfo.userCompanyGuid;
            new DBIotProduct().saveAll(LoginUserInfo, product);
            return RespResult.Sucess();
        }

    }
}